package com.cl.mypureframe.util;

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

/**
 * @author zhuchaoxu
 */
public final class UtilFile {

    private UtilFile() {
    }

    public static boolean createPathFile(String... filePaths) {

        boolean m = true;

        for (String filePath : filePaths) {

            File f = new File(filePath);

            if (!f.exists()) {

                if (f.mkdirs()) {

                    Log.e("UtilFile", f.getPath() + "创建成功");

                } else {

                    Log.e("UtilFile", f.getPath() + "创建失败");

                    m = false;

                }

            } else {

                Log.e("UtilFile", f.getPath() + "已存在");

            }

        }

        return m;

    }

    public static boolean deletePathFile(String filePath) {

        return new File(filePath).delete();

    }

    public static void copyFile(String paht, String newPath) {

        try {

            int bm = 0;

            int bd = 0;

            File of = new File(paht);

            if (of.exists()) {

                InputStream is = new FileInputStream(paht);

                FileOutputStream fos = new FileOutputStream(newPath);

                byte[] b = new byte[1024];

                while ((bd = is.read(b)) != -1) {

                    bm += bd;

                    System.out.println(bm);

                    fos.write(b, 0, bd);

                }

                fos.close();

                is.close();

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public static void zipFiles(File oldFile, File newFile) {

        try {

            ZipOutputStream z = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(newFile), 1024 * 1024));

            zipFile(oldFile, z, "");

            z.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) {

        try {

            int BUFF_SIZE = 1024 * 1024;

            rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) + resFile.getName();

            rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");

            if (resFile.isDirectory()) {

                File[] fl = resFile.listFiles();

                for (File f : fl) {

                    zipFile(f, zipout, rootpath);

                }

            } else {

                byte b[] = new byte[BUFF_SIZE];

                BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE);

                zipout.putNextEntry(new ZipEntry(rootpath));

                int r;

                while ((r = in.read(b)) != -1) {

                    zipout.write(b, 0, r);

                }

                in.close();

                zipout.flush();

                zipout.closeEntry();

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    @SuppressWarnings("resource")
    public static void upZipFile(File zipFile, String folderPath) {

        try {

            File d = new File(folderPath);

            if (!d.exists()) {

                d.mkdirs();

            }

            ZipFile zf = new ZipFile(zipFile);

            for (Enumeration<?> en = zf.entries(); en.hasMoreElements(); ) {

                ZipEntry e = ((ZipEntry) en.nextElement());

                InputStream in = zf.getInputStream(e);

                String str = folderPath + File.separator + e.getName();

                str = new String(str.getBytes("8859_1"), "GB2312");

                File f = new File(str);

                if (!f.exists()) {

                    File p = f.getParentFile();

                    if (!p.exists()) {

                        p.mkdirs();

                    }

                    f.createNewFile();

                }

                OutputStream out = new FileOutputStream(f);

                byte b[] = new byte[1024 * 1024];

                int r;

                while ((r = in.read(b)) > 0) {

                    out.write(b, 0, r);

                }

                in.close();

                out.close();

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    @SuppressWarnings("unused")
    private static void deleteFile(File file) {

        try {

            if (!file.exists()) {

                throw new Exception(file.getName());

            }

            if (file.isFile()) {

                if (file.canWrite()) {

                    file.delete();

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public static boolean deleteAllFile(String path) {

        boolean f = false;

        File l = new File(path);

        if (!l.exists()) {

            return f;

        }

        if (!l.isDirectory()) {

            return f;

        }

        String[] t = l.list();

        File p;

        for (int i = 0; i < t.length; i++) {

            if (path.endsWith(File.separator)) {

                p = new File(path + t[i]);

            } else {

                p = new File(path + File.separator + t[i]);

            }
            if (p.isFile()) {

                p.delete();

            }

            if (p.isDirectory()) {

                deleteAllFile(path + "/" + t[i]);

                deleteFolder(path + "/" + t[i]);

                f = true;

            }

        }

        return f;

    }

    public static void deleteFolder(String folderPath) {

        try {

            deleteAllFile(folderPath);

            new File(folderPath).delete();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public static void copyFolder(String oldPath, String newPath) {

        try {

            (new File(newPath)).mkdirs();

            File a = new File(oldPath);

            String[] f = a.list();

            File t = null;

            for (int i = 0; i < f.length; i++) {

                if (oldPath.endsWith(File.separator)) {

                    t = new File(oldPath + f[i]);

                } else {

                    t = new File(oldPath + File.separator + f[i]);

                }

                if (t.isFile()) {

                    FileInputStream fis = new FileInputStream(t);

                    FileOutputStream fos = new FileOutputStream(newPath + "/" + (t.getName()).toString());

                    byte[] b = new byte[1024 * 5];

                    int len;

                    while ((len = fis.read(b)) != -1) {

                        fos.write(b, 0, len);

                    }

                    fos.flush();

                    fos.close();

                    fis.close();

                }
                if (t.isDirectory()) {

                    copyFolder(oldPath + "/" + f[i], newPath + "/" + f[i]);

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public static List<File> getFileSort(String path) {

        List<File> l = getFiles(path, new ArrayList<File>());

        if (l != null && l.size() > 0) {

            Collections.sort(l, new Comparator<File>() {

                public int compare(File f, File nf) {

                    if (f.lastModified() < nf.lastModified()) {

                        return 1;

                    } else if (f.lastModified() == nf.lastModified()) {

                        return 0;

                    } else {

                        return -1;

                    }

                }

            });

        }

        return l;

    }

    private static List<File> getFiles(String realpath, List<File> files) {

        File r = new File(realpath);

        if (r.isDirectory()) {

            for (File f : r.listFiles()) {

                if (f.isDirectory()) {

                    getFiles(f.getAbsolutePath(), files);

                } else {

                    files.add(f);

                }

            }

        }

        return files;

    }

}
